<template>
  <view class="add-passenger">
    <view style="height: 30px" v-if="isEdit" @click="deletePassenger(submitInfo.id)">
      <uni-icons type="trash" size="20" style="float: right"/>
    </view>
    <view class="container">
      <uni-list>
        <uni-list-item>
          <template slot="header">
            <view class="label">中文姓名</view>
          </template>
          <template slot="body">
            <input placeholder="与证件名一致" v-model="submitInfo.passengerName"/>
          </template>
        </uni-list-item>
        <view class="line"></view>
        <uni-list-item>
          <template slot="header">
            <view class="label">手机号</view>
          </template>
          <template slot="body">
            <input placeholder="接收行程信息手机号" v-model="submitInfo.passengerTel"/>
            <!--          <uni-easyinput placeholder="接收行程信息手机号"/>-->
          </template>
        </uni-list-item>
      </uni-list>
    </view>
    <button type="primary" style="margin: 20rpx" @click="submitData">保存</button>
  </view>
</template>

<script>
import {addPassenger, delPassenger, editPassenger} from "@/utils/api/basics";

export default {
  onLoad(opt) {
    if (opt.id != undefined) {
      this.isEdit = true
      this.submitInfo.id = opt.id
      this.submitInfo.passengerName = opt.passengerName
      this.submitInfo.passengerTel = opt.passengerTel
    }
    this.submitInfo.uid = opt.uid
  },
  data() {
    return {
      submitInfo: {
        passengerName: '',
        passengerTel: ''
      },
      isEdit: false
    };
  }, methods: {
    deletePassenger(id) {
      delPassenger(id).then(res => {
        if (res.data.code == 200) {
          uni.showToast({
            title: '删除成功',
          });
          uni.$emit('updatePassengers')
          uni.navigateBack()
        }
      })
    },
    submitData() {
      if (this.isEdit) {
        editPassenger(this.submitInfo).then(res => {
          if (res.data.code == 200) {
            uni.showToast({
              title: '添加成功',
            });
            uni.$emit('updatePassengers')
            uni.navigateBack()
          }
        })
      } else {
        addPassenger(this.submitInfo).then(res => {
          if (res.data.code == 200) {
            uni.showToast({
              title: '添加成功',
            });
            uni.$emit('updatePassengers')
            uni.navigateBack()
          }
        })
      }
    }
  }
}
</script>

<style lang="scss">
.add-passenger {
  input {
    outline: none;
  }

  .label {
    width: 200rpx;
  }
}

button[type=primary] {
  background: #007aff;
}

.line {
  background: #f8f8f8;
  height: 20px;
}

</style>
